<template>
	<view class="page">
		<view style="background-color: #fff;width: 100%;">
			<view class="phonenumber">
				<span class="sp_1">姓名 <span class="sp_2">*</span> </span>
				<input type="text" v-model="this.name" class="inp_1" placeholder="请输入姓名">
			</view>
			<view class="phonenumber">
				<span class="sp_1">手机号 <span class="sp_2">*</span> </span>
				<input type="text" v-model="this.Phone" class="inp_1" placeholder="请输入手机号">
			</view>
			<view class="phonenumber">
				<span class="sp_1">当前地址<span class="sp_2">*</span> </span>
				<input type="text" v-model="this.Address" class="inp_1" placeholder="请输入地址">
			</view>
			<view class="phonenumber">
				<span class="sp_1">阴阳性<span class="sp_2">*</span> </span>
							<view class="select">
								<uni-data-select 
								       v-model="value"
								       :localdata="range"
									   placeholder="请选择检测结果"
								     ></uni-data-select>
							</view>
							 
			</view>
			
		</view>

		<view class="img_1">
			<image :src="capturedPhoto" mode="aspectFill"></image>
		</view>
		<button @click="takePhoto" class="bt_1">拍照</button>
		<button @click="onsit" class="bt_1">上传结果</button>
	</view>
</template>

<script>
	import {
		ConvertCoordinate,
		Upload02,
		CreateGeneralDetection,
		OCR
	} from '@/utils/api.js'
	export default {
		data() {
			return {
				gps: false,
				capturedPhoto: '', // 初始化为空
				name:'',
				Phone: '', //当前手机号
				Address: "", //当前地址
				base64: '', //当前图片文件流
				filename: '',
				deviceId:'',//ocr识别返回的试剂编号
				contentText:'',
				value: "",
				       range: [
				         { value: "阴性", text: "阴性" },
				         { value: "阳性", text: "阳性" },
				       ],
			}
		},
		onLoad() {
			this.Phone = this.$store.state.vx_u_info.Phone
			console.log('当前手机号', this.Phone);
			this.getLocation()
		},
		methods: {
			//上传结果
			onsit() {
                console.log('当前数据',this.base64);
				Upload02({
					base64Data:(this.base64).toString(),
					fileName:this.Phone+'检测结果.jpg'
				}).then(res=>{
					console.log(res);
					if(res.Code==200){
						let wenjianID =res.Data.Id
						if(this.name!=''&&this.Phone!=''&&this.Address!=''){
							OCR(wenjianID).then(res=>{
								console.log('ocr识别结果',res);
								if(res.Code==200){
									this.deviceId =res.Data
									this.contentText = "姓名:"+this.name+"\n"+"地址:"+this.Address+"\n"+"手机号:"+this.Phone+"\n"+"试剂编号:"+this.deviceId
									let contentInfo = this.contentText.replace(/<br>/g,"\n")
										                             
										uni.showModal({
											title: "请确认信息无误",
											content: contentInfo,
											confirmText: "提交",
											showCancel: true,
											success: (res) => {	
												CreateGeneralDetection(
												{
													Type:'HIV普通检测',
													FileId:wenjianID,
													UserId:this.$store.state.vx_uid,
													Address:this.Address,
													Phone:this.Phone,
													Name:this.name,
													DeviceNo:this.deviceId,
													Result:this.value
												}).then(res=>{
													console.log('创建检测结果接口',res);
													if(res.Code==200){
														uni.showModal({
															content: '上传成功。',
															showCancel: false,
															success(res) {
																if (res.confirm) {
																	uni.navigateBack()
																}
															}
														})
													
													}
												}).catch(err=>{
													console.log(err);
												})
											}
										})

								}
							}).catch(res=>{
								uni.showToast({
									title: `文件不存在`,
									icon: 'error',
									duration: 2000
								})
							})
						}else{
							uni.showToast({
								title: `请填写完整信息`,
								icon: 'error',
								duration: 2000
							})
						}
						
					}
				}).catch(err=>{
					console.log(err);
					uni.showModal({
						content: '上传失败，请检查网络连接',
						success(res) {
						}
					})
				})
			},
			//拍照
			takePhoto() {
				var that =this
				uni.chooseMedia({
					count: 9,
					mediaType: ['image'],
					sourceType: ['album', 'camera'],
					maxDuration: 30,
					camera: 'back',
					success: (res) => {
						console.log(res);
						that.filename = res.tempFiles[0].tempFilePath
						that.capturedPhoto = res.tempFiles[0].tempFilePath;
						//通过uni.getFileSystemManager().readFile方法读取选取的图片文件，
						//并以base64格式获取图片数据。最后，将base64编码的图片数据转换为HTTP文件流形式。
						uni.getFileSystemManager().readFile({
							filePath: that.capturedPhoto,
							encoding: 'base64', // 以base64格式读取图片文件
							success: function(data) {
								const binaryData = data.data; // 获取图片的二进制数据
								that.base64 = binaryData
								// 现在可以将fileStream发送给服务器或进行其他处理
								console.log('拍照结束的二进制数据：', that.base64);
							},
							fail: function(error) {
								console.error('读取文件失败：', error);
							}
						});
					},
					fail: (error) => {
						console.error('选择媒体文件失败:', error);
					}
				});
			},
			//定位
			getLocation(tag) {
				uni.getLocation({
					type: 'wgs84',
					success: async (res) => {
						//res.longitude,res.latitude分别为当前经度，纬度
						const response = await ConvertCoordinate({
							Locations: [res.longitude, res.latitude].join(',')
						})
						console.log(response.Data.regeocode)
						if (response.Code === 200) {
							const {
								province,
								city,
								district,
								township
							} = response.Data.regeocode.addressComponent
							const address = response.Data.regeocode.formatted_address
							this.Address = address
							this.$store.commit('$uStore', {
								name: 'vx_city',
								value: {
									province,
									city,
									district,
									township,
									address
								}
							})
							this.gps = true
						}
					},
					fail: (err) => {
						console.log(err)
						this.gps = false
						uni.showModal({
							title: '提示',
							content: '使用本设备时，请开启手机定位！',
							showCancel: false,
							success: (res) => {
								if (res.confirm) {
									console.log('用户点击确定');
									uni.openSetting({
										success: function(res) {
											// 在成功回调中判断用户是否已开启位置权限，并进行相应处理
											if (res.authSetting[
													'scope.userLocation']) {
												// 用户已开启位置权限，可以重新调用 uni.getLocation 获取位置信息
												console.log('调用');
											}
										}
									});
								}
							}
						});
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		width: 100vw;
		height: 100%;
		position: relative;
		display: flex;
		flex-direction: column;
		overflow: auto;

		.bt_1 {
			width: 622rpx;
			height: 98rpx;
			background: #1677FF;
			margin: 35rpx auto;
			font-size: 36rpx;
			font-family: Source Sans Pro, Source Sans Pro;
			font-weight: 400;
			color: #FFFFFF;
			line-height: 98rpx;
		}

		.img_1 {
			width: 300rpx;
			// max-width: 600px; /* 设置最大宽度 */
			height: 300rpx;
			margin: 10rpx auto;

			// background-color: #1677FF;
			image {
				width: 100%;
				height: 100%;
			}
		}

		.phonenumber {
			width: 686rpx;
			height: 112rpx;
			border-bottom: 1rpx solid #E7E7E7;
			margin-left: 32rpx;

			.sp_1 {
				display: inline-block;
				margin-top: 32rpx;
				font-weight: 400;
				width: 200rpx;
				height: 48rpx;
				float: left;
				font-size: 32rpx;
				line-height: 48rpx;

				.sp_2 {
					color: red;
				}
			}

			.inp_1 {
				float: right;
				height: 70rpx;
				margin-top: 20rpx;
				border-radius: 10rpx;
				padding-left: 10rpx;
				border: 1rpx solid #e5e5e5;
			}
	.select{
				width: 350rpx;
				float: right;
				margin-top: 25rpx;
			}

		}
	}
</style>